﻿using System;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.ArticleCategories
{
    public partial class Index : ManageBasePage
    {
        private IArticleCategoryTask _articleCatTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("ArticleCategoryIndex");
            _articleCatTask = GetInstance<IArticleCategoryTask>();

            if (!IsPostBack)
            {
                rptCats.DataSource = _articleCatTask.GetAll();
                rptCats.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var catName = txtCategoryName.Text.Trim();

            if(string.IsNullOrEmpty(catName))
            {
                ShowMsg("输入不完整");
                return;
            }

            var cmd = new ArticleCategoryCommand
                          {
                               CategoryName = catName
                          };
            using (var work = UnitOfWork.Begin())
            {
                if(_articleCatTask.Save(cmd)!= null)
                {
                    work.Commit();
                    ShowMsg("新增成功","Index.aspx");
                }
                else
                {
                    ShowMsg("新增失败");
                }
            }

        }
    }
}